package com.sth.compose;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式是结构型模式,因为它创建了一组对象的树结构
 * 组合模式将一组对象视为单个对象
 * 组合模式使用一个类表示树结构
 * 在组合模式中,我们创建了一个包含自己对象的类的组合
 */
public class AppMain {
    public static void main(String[] args) {
        Employee ceo = new Employee("sth", "CEO");
        Employee headSales = new Employee("Rob", "headSales");
        Employee headMarketing = new Employee("Mike", "headMarketing");
        Employee progammer1 = new Employee("Lily", "progammer1");
        Employee progammer2 = new Employee("Bob", "progammer2");
        Employee test1 = new Employee("Jack", "test1");
        Employee test2 = new Employee("Tom", "test2");


        ceo.add(headMarketing);
        ceo.add(headSales);

        headSales.add(test1);
        headSales.add(test2);

        headMarketing.add(progammer1);
        headMarketing.add(progammer2);
        System.out.println("CEO---" + ceo);
        // print all employee of the organization

        for (Employee headEmployee : ceo.getSubbordinates()) {
            System.out.println(headEmployee);
            for (Employee employee : headEmployee.getSubbordinates()) {
                System.out.println(employee);
            }
        }
    }
}

class Employee {
    private String name;
    private String title;
    private List<Employee> subbordinates;

    public Employee(String name, String title) {
        this.name = name;
        this.title = title;
        subbordinates = new ArrayList<Employee>();
    }

    public void add(Employee employee) {
        subbordinates.add(employee);
    }

    public void remove(Employee employee) {
        subbordinates.remove(employee);
    }

    public List<Employee> getSubbordinates() {
        return subbordinates;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name :'" + name + '\'' +
                ", dept :'" + title + '\'' + subbordinates +
                '}';
    }
}